package com.useeinfo.oa.modules.security.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.security.entity.UserDevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("UserDeviceDao")
public class UserDeviceDao implements CrudDao<UserDevice> {
    private final static Logger logger = LoggerFactory.getLogger(UserDeviceDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    // ******************************************************************************
    // ******************************* 代码自动生成开始 *******************************
    // ******************************************************************************

    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {

        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String userId = queryHash.get("userId");
        if (!StringUtils.isEmpty(userId)) {
            conditionHash.put("systemUser.id = ?{paramIndex}", StringConverters.null2Long(userId));
        }
        String userIds = queryHash.get("userIds");
        if (!StringUtils.isEmpty(userIds)) {
            conditionHash.put("systemUser.id in ?{paramIndex}",userIds);
        }
        String deviceToken = queryHash.get("deviceToken");
        if (!StringUtils.isEmpty(deviceToken)) {
            conditionHash.put("deviceToken = ?{paramIndex}", deviceToken);
        }
        String deviceType = queryHash.get("deviceType");
        if (!StringUtils.isEmpty(deviceType)) {
            conditionHash.put("deviceType = ?{paramIndex}", deviceType);
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(u) from UserDevice u ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<UserDevice> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select u from UserDevice u ", "order by u.id desc ");

        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<UserDevice> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, UserDevice.class);

        return queryParam.findPageList(typedQuery);
    }

    @Override
    public UserDevice findModel(Long id) {
        return entityManager.find(UserDevice.class, id);
    }

    @Override
    public Integer add(UserDevice model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("UserDeviceImpl添加UserDevice成功！");
        return 1;
    }

    @Override
    public Integer update(UserDevice model) {
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        UserDevice userDevice = entityManager.find(UserDevice.class, id);
        entityManager.remove(userDevice);
        return 1;
    }

    @Override
    public Integer deleteLogic(UserDevice model) {
        model.setDeleteStatus(true);
        return 1;
    }


    //通过报警id获取设备人员
    public List getUserDeviceListByWarningGroupId(Long warningGroupId) {
        String sql = "SELECT device_type,device_token FROM user_device\n" +
                "WHERE delete_status=FALSE AND user_id IN(SELECT user_id FROM warning_group_inner_user WHERE warning_group_id=" + warningGroupId + ")";
        return entityManager.createNativeQuery(sql).getResultList();
    }

}
